package cn.hx.whh.automaticticket.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;
import cn.hx.whh.automaticticket.R;

public class TipsDialog {
    public Dialog dialog;

    private TextView tips;

    private Timer timer;
    private TimerTask task;
    /**
     *
     * @param mActivity
     * @param tips  提示语
     * @param isAutoOff  是否5秒后自动关闭 true: 是 , false: 否
     */
    public TipsDialog(Activity mActivity, String tips, boolean isAutoOff) {
        this.dialog = new Dialog(mActivity, R.style.mask_dialog);
        LinearLayout popView = (LinearLayout) LayoutInflater.from(mActivity).inflate(R.layout.tips_dialog_view, null);
        this.dialog.setContentView(popView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        this.dialog.setFeatureDrawableAlpha(Window.FEATURE_OPTIONS_PANEL, 0);
        this.tips = (TextView) popView.findViewById(R.id.tips);
        this.tips.setText(tips);
        if (isAutoOff){
            this.timer = new Timer(true);
            this.task = new TimerTask() {
                public void run() {
                    switchActivity();
                }
            };
            init();
        }
    }

    private void init() {
        timer.schedule(task, 3000  );
    }

    private void switchActivity() {
        hide();
    }

    public void show() {
        dialog.show();
    }

    public void hide() {
        dialog.dismiss();
        dialog = null;
    }
}